import csv
import math
import os

CSV_NAME = "danbooru_tags_post_count.csv"
CSV_PATH = os.path.join(os.path.dirname(__file__), CSV_NAME)


def main():
    if not os.path.exists(CSV_PATH):
        print(f"CSV not found: {CSV_PATH}")
        return

    total = 0
    total_log = 0.0
    n = 0
    min_count = None
    max_count = None

    with open(CSV_PATH, "r", encoding="utf-8", newline="") as f:
        reader = csv.reader(f)
        header = next(reader, None)
        for row in reader:
            if not row or len(row) < 2:
                continue
            try:
                cnt = int(row[1])
            except Exception:
                continue
            total += cnt
            n += 1
            v = max(cnt, 1)
            total_log += math.log10(v)
            if min_count is None or cnt < min_count:
                min_count = cnt
            if max_count is None or cnt > max_count:
                max_count = cnt

    if n == 0:
        print("No rows")
        return

    avg = total / n
    x_avg = total_log / n
    x_min = math.log10(max(min_count or 1, 1))
    x_max = math.log10(max(max_count or 1, 1))

    print("Stats for", CSV_NAME)
    print("Rows:", n)
    print("Avg count:", avg)
    print("Min count:", min_count)
    print("Max count:", max_count)
    print("Log10 avg:", x_avg)
    print("Log10 min:", x_min)
    print("Log10 max:", x_max)


if __name__ == "__main__":
    main()
